package com.zksk.admin.service.customer.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.admin.domain.customer.vo.CustomerFVo;
import com.zksk.admin.domain.customer.vo.CustomerVo;
import com.zksk.admin.service.customer.CustomerService;
import com.zksk.common.core.domain.R;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.Customer;
import com.zksk.mbg.admin.domain.CustomerProdReq;
import com.zksk.mbg.admin.mapper.CustomerMapper;
import com.zksk.mbg.admin.mapper.CustomerProdReqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @auther xiebo
 * @date 2022/12/2
 **/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerProdReqMapper customerProdReqMapper;


    /**
     * 分页条件查询客户信息
     * @param customerFVo
     * @return
     */
    @Override
    public R<Page<CustomerVo>> selectList(CustomerFVo customerFVo) {

        Page<CustomerVo> voPage = new Page<>();
        Page<Customer> customerPage = customerMapper.selectPage
                (
                        //分页
                        new Page<>(customerFVo.getPageNum(), customerFVo.getPageSize()),
                        //条件构造
                        new LambdaQueryWrapper<Customer>()
                                .like(StrUtil.isNotEmpty(customerFVo.getCustomerName()),Customer::getCustomerName,customerFVo.getCustomerName())
                                .like(StrUtil.isNotEmpty(customerFVo.getMergerName()),Customer::getMergerName,customerFVo.getMergerName())
                                .eq(StrUtil.isNotEmpty(customerFVo.getCustomerType()),Customer::getCustomerType,customerFVo.getCustomerType())
                );
        //实体转Vo
        BeanUtil.copyProperties(customerPage,voPage);
//        //分页数据转Vo
//        voPage.setRecords(CglibUtil.copyList(customerPage.getRecords(), CustomerVo::new));
        return R.ok(voPage);
    }


    /**
     * 新增客户
     * @param customerVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insert(CustomerVo customerVo) {

        //如果传了ID设置为Null,主要针对swagger测试时忘了删除ID
        customerVo.setCustomerId(null);
        //设置为存在
        customerVo
                //设置为上线
                .setStatus(0)
                //设置为存在
                .setDelFlag(0)
                //设置创建人
                .setCreateBy(SecurityUtils.getUsername());

        Customer customer = BeanUtil.copyProperties(customerVo, Customer.class);

        int insert = customerMapper.insert(customer);


        //修改绑定关系
        this.updateBind(customer.getCustomerId(),customerVo.getBindIds());

        //TODO 绑定平台

        return R.ok("新增" + insert +"条记录！","新增成功!");
    }

    /**
     * 校验客户名称
     * @param customerName
     * @return
     */
    @Override
    public boolean checkUserNameUnique(String customerName) {
        if (CollUtil.isNotEmpty(customerMapper.selectList(new QueryWrapper<Customer>().eq(Customer.CUSTOMER_NAME, customerName))))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 校验联系人
     * @param contacts
     * @return
     */
    @Override
    public boolean checkContacts(String contacts) {
        if (CollUtil.isNotEmpty(customerMapper.selectList(new QueryWrapper<Customer>().eq(Customer.CONTACTS, contacts))))
        {
            return true;
        }
        else
        {
            return false;

        }
    }

    /**
     * 校验联系人手机号
     * @param contactsPhone
     * @return
     */
    @Override
    public boolean checkContactsPhone(String contactsPhone) {
        if (CollUtil.isNotEmpty(customerMapper.selectList(new QueryWrapper<Customer>().eq(Customer.CONTACTS_PHONE, contactsPhone))))
        {
            return true;
        }
        else
        {
            return false;

        }
    }

    /**
     * 修改客户和信息
     * @param customerVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateCustomer(CustomerVo customerVo) {

        //设置修改人
        customerVo.setUpdateBy(SecurityUtils.getUsername());
        //修改绑定关系
        this.updateBind(customerVo.getCustomerId(),customerVo.getBindIds());

        //TODO 绑定平台

        return R.ok("修改" + customerMapper.updateById(BeanUtil.copyProperties(customerVo, Customer.class)) + "条数据!","修改成功!");
    }


    /**
     * 修改客户产品表中客户ID
     * @param customerId
     * @param bindIds
     */
    private void updateBind(Long customerId, List<Long> bindIds) {

        //查询所有要修改的实体
        List<CustomerProdReq> customerProdReqs = customerProdReqMapper.selectList(
                new LambdaQueryWrapper<CustomerProdReq>()
                        .in(CustomerProdReq::getId, bindIds)
        );
        //修改客户产品需求绑定客户的ID
        customerProdReqs.forEach(customerProdReq ->
        {
            customerProdReq.setCustomerId(customerId)
                    .setUpdateBy(SecurityUtils.getUsername());
            customerProdReqMapper.updateById(customerProdReq);
        });
    }



}
